package indexer;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import searcher.Hit;
import searcher.HitSet;

public class SimpleQuerer {
	private ReverseIndex ri;
	private Vector<Integer> filesFound;
	
	public SimpleQuerer (ReverseIndex ri_in) {
		ri=ri_in;
	}
	
	public void referIndex(ReverseIndex ri_in){
		ri=ri_in;
	}
	
	public void getIndex(ReverseIndex ri_in) throws IOException{
		ri_in.storeIndex(System.getProperty("java.io.tmpdir"));
		ri=new ReverseIndex();
		ri.loadIndex(System.getProperty("java.io.tmpdir"));
		// this is obviously the most stupid method
	}
	
	public void simpleQuery (List<String> terms){
		Vector<Integer> fSN,tmpVec;
		fSN = ri.filesContainsWord(terms.get(0));
		for (int i = 1; i < terms.size(); i++) {
			tmpVec = ri.filesContainsWord(terms.get(i));
			fSN = intersection(fSN, tmpVec);
		}
		filesFound=fSN;
	}
	
	public Vector<Integer> returnFilesFound(){
		Enumeration<Integer> en=filesFound.elements();
		Integer tempint;
		Vector<Integer> vecout=new Vector<Integer>();
		while (en.hasMoreElements()){
			tempint=en.nextElement();
			vecout.add(tempint);
		}
		return vecout;
	}

	/**
	 * let upper level to handle it, here, we just return what we found.
	 * @author aleck
	 * @return
	 */
	public HitSet getResults() {
		if (filesFound == null){
			return null;
		}
		Enumeration <Integer> en = filesFound.elements();
		Integer tempint;
		HitSet ret = new HitSet();
		while (en.hasMoreElements()){
			tempint = en.nextElement();
			ret.addHit(new Hit(ri.queryFileSN(tempint)));
		}
		return ret;
	}
	
	public void printFilesFound (){
		if (filesFound==null){
			System.out.println("no files found");
			return;
		}
		Enumeration<Integer> en=filesFound.elements();
		Integer tempint;
		while (en.hasMoreElements()){
			tempint=en.nextElement();
			System.out.println(ri.queryFileSN(tempint));
		}
	}
	
	
	//get more than two sets' intersection at one time can be much quickly
	//at least when the sets are sorted
	public Vector<Integer> intersection(Vector<Integer> vec1,Vector<Integer> vec2){
		if (vec1==null || vec2==null)
			return null;
		Vector<Integer> vecout=new Vector<Integer>();
		Enumeration<Integer> en=vec1.elements();
		Integer tempint;
		while (en.hasMoreElements()){
			tempint=en.nextElement();
			if (vec2.contains(tempint)){
				vecout.add(tempint);
			}
		}
		return vecout;
	}
	
	public Vector<Integer> difference(Vector<Integer> vec1,Vector<Integer> vec2){
		if (vec1==null)
			return null;
		Vector<Integer> vecout=new Vector<Integer>();
		Enumeration<Integer> en=vec1.elements();
		Integer tempint;
		while (en.hasMoreElements()){
			tempint=en.nextElement();
			if (!vec2.contains(tempint)){
				vecout.add(tempint);
			}
		}
		return vecout;
	}
	
	
	//union will change the sorted vectors into unsorted
	public Vector<Integer> union(Vector<Integer> vec1,Vector<Integer> vec2){
		if (vec1==null){
			return new Vector<Integer>(vec2);
		}
		Vector<Integer> vecout=new Vector<Integer>(vec1);
		Enumeration<Integer> en=vec2.elements();
		Integer tempint;
		while (en.hasMoreElements()){
			tempint=en.nextElement();
			if (!vec1.contains(tempint)){
				vecout.add(tempint);
			}
		}
		return vecout;
	}
}
